package com.huluobo.high3demo.live;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm.baen.FollowLikeBeanResult;
import com.huluobo.mvvm.baen.GiftBeanResult;
import com.huluobo.mvvm.db.eneity.Money;
import com.huluobo.mvvm.model.BaseRepo;

import org.reactivestreams.Subscription;

import io.reactivex.FlowableSubscriber;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by LC on 2023/9/21.
 */
public class LiveRepo extends BaseRepo {

    /**
     * 获取礼物列表
     *
     * @param success 成功的livedata
     * @param failed  失败的livedata
     */
    public void getGiftList(MutableLiveData<GiftBeanResult> success, MutableLiveData<String> failed) {
        apiService.getGiftList().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GiftBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GiftBeanResult giftBeanResult) {
                        success.setValue(giftBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void followAuth(String authName, MutableLiveData<FollowLikeBeanResult> success,
                           MutableLiveData<String> failed) {
        apiService.followAuth(authName).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowLikeBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowLikeBeanResult followLikeBeanResult) {
                        success.setValue(followLikeBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                        Log.i("TagA", "e:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 查询用户余额
     *
     * @param username 查询的key
     * @param success  成功的回调
     * @param failed   失败的回调
     */
    public void queryUserMoney(String username, MutableLiveData<Money> success, MutableLiveData<String> failed) {
        moneyDao.queryUserMoney(username).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Money>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Money money) {
                        success.setValue(money);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 更新用户余额
     *
     * @param money 剩余的钱
     */
    public void updateUserMoney(Money money) {
        moneyDao.updateUserMoney(money).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();
    }
}
